package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.entity.MovieInstance;
import la.iok.hzsvn.lewin.movie.model.ClueCardVo;
import la.iok.hzsvn.lewin.movie.service.ClueCardInstanceService;
import la.iok.hzsvn.lewin.movie.service.MovieDownloadService;
import la.iok.hzsvn.lewin.movie.service.MovieInstanceService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("clueCardInstances")
public class ClueCardInstanceController {
    private final MovieDownloadService movieDownloadService;
    private final ClueCardInstanceService clueCardInstanceService;
    private final MovieInstanceService movieInstanceService;

    public ClueCardInstanceController(MovieDownloadService movieDownloadService,
                                      ClueCardInstanceService clueCardInstanceService,
                                      MovieInstanceService movieInstanceService) {
        this.movieDownloadService = movieDownloadService;
        this.clueCardInstanceService = clueCardInstanceService;
        this.movieInstanceService = movieInstanceService;
    }

    @GetMapping("movie/{id}")
    public ListDataResponse<ClueCardVo> movieClueCards(@RequestHeader Token token, @PathVariable Long id){
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,id);
        return new ListDataResponse<>(clueCardInstanceService.clueCards(movieInstanceId));
    }

    @GetMapping("movie/{id}/v/{version}")
    public ListDataResponse<ClueCardVo> movieClueCards2(@PathVariable Long id,@PathVariable String version){
        MovieInstance mi = movieInstanceService.movieInst2(id,version);
        return new ListDataResponse<>(clueCardInstanceService.clueCards(mi.getId()));
    }

    @GetMapping("movie/{movieId}/clueCard/{id}/hdDigest")
    public SingleDataResponse<String> hdDigest(@RequestHeader Token token,@PathVariable Long movieId,@PathVariable Long id){
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,movieId);
        String digest = clueCardInstanceService.hdFileDigest(movieInstanceId,id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(digest);
        return res;
    }

    @GetMapping("movie/{movieId}/v/{version}/clueCard/{id}/hdDigest")
    public SingleDataResponse<String> hdDigest2(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        String digest = clueCardInstanceService.hdFileDigest(mi.getId(),id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(digest);
        return res;
    }

    @GetMapping("movie/{movieId}/v/{version}/clueCard/{id}/hdStorePath")
    public SingleDataResponse<String> hdStorePath(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        String storePath = clueCardInstanceService.hdFileStorePath(mi.getId(),id);
        return OperatorResponses.stringSingleDataResponse(storePath);
    }

    @GetMapping("movie/{movieId}/clueCard/{id}/sdDigest")
    public SingleDataResponse<String> sdDigest(@RequestHeader Token token,@PathVariable Long movieId,@PathVariable Long id){
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,movieId);
        String digest = clueCardInstanceService.sdFileDigest(movieInstanceId,id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(digest);
        return res;
    }

    @GetMapping("movie/{movieId}/v/{version}/clueCard/{id}/sdDigest")
    public SingleDataResponse<String> sdDigest2(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        String digest = clueCardInstanceService.sdFileDigest(mi.getId(),id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(digest);
        return res;
    }

    @GetMapping("movie/{movieId}/v/{version}/clueCard/{id}/sdStorePath")
    public SingleDataResponse<String> sdStorePath(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        String storePath = clueCardInstanceService.sdFileStorePath(mi.getId(),id);
        return OperatorResponses.stringSingleDataResponse(storePath);
    }

    @GetMapping("movie/{movieId}/clueCard/{id}/sdFile")
    public void downloadSdFile(@RequestHeader Token token,@PathVariable Long movieId, HttpServletResponse response, @PathVariable Long id){
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,movieId);
        clueCardInstanceService.downloadSdFile(response,movieInstanceId,id);
    }

    @GetMapping("movie/{movieId}/v/{version}/clueCard/{id}/sdFile")
    public void downloadSdFile2(@PathVariable Long movieId, @PathVariable String version, HttpServletResponse response, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        clueCardInstanceService.downloadSdFile(response,mi.getId(),id);
    }

    @GetMapping("movie/{movieId}/clueCard/{id}/hdFile")
    public void downloadHdFile(@RequestHeader Token token,@PathVariable Long movieId, HttpServletResponse response, @PathVariable Long id){
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,movieId);
        clueCardInstanceService.downloadHdFile(response,movieInstanceId,id);
    }

    @GetMapping("movie/{movieId}/v/{version}/clueCard/{id}/hdFile")
    public void downloadHdFile2(@PathVariable Long movieId, @PathVariable String version, HttpServletResponse response, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        clueCardInstanceService.downloadHdFile(response,mi.getId(),id);
    }
}
